package com.yfve.eol.util;

import android.annotation.SuppressLint;
import android.hardware.YfveEolManager;
import android.util.Log;

import com.yfve.eol.Constants;
import com.yfve.eol.EolApplication;

import java.util.Arrays;

public class ScreenEolUtil {
    private static final String TAG = "ScreenEolUtil";
    private static final String YFVE_EOL_SERVICE = "YfveEolService";
    private static ScreenEolUtil sInstance;
    private YfveEolManager mYfveEolManager;

    @SuppressLint("WrongConstant")
    private ScreenEolUtil() {
        mYfveEolManager = (YfveEolManager) EolApplication.getAppContext().getSystemService(YFVE_EOL_SERVICE);
    }

    public static ScreenEolUtil getInstance() {
        if (sInstance == null) {
            sInstance = new ScreenEolUtil();
        }
        return sInstance;
    }

    //check the error
//correct info : OK
//error info :
// "Watchdog Timer",
// "Reserved",
// "Vdda Low Voltage",
// "Configuration CRC Error",
// "Bootloader CRC Error",
// "Application CRC Error",
// "Invalid Configuration",
// "Automatic Short Test",
// "Automatic Open Test",
    public void handleGetTPSts(int id) {
        Log.i(TAG, "handleGetTPSts: id = " + id);

        byte[] resultBytes = new byte[2];
        byte[] tpErrBytes = mYfveEolManager.getTpErr();
        if (tpErrBytes == null || tpErrBytes.length == 0) {
            Log.e(TAG, "handleGetTPSts: null tp err");
            resultBytes[0] = Constants.VAL_0X01;
        } else {
            resultBytes[0] = Constants.VAL_0X00;
            String tpErrStr = new String(tpErrBytes);
            Log.e(TAG, "handleGetTPSts: tpErrStr = " + tpErrStr);
            String okStr = "OK";
            if (tpErrStr.toUpperCase().contains(okStr)) {
                resultBytes[1] = Constants.VAL_0X01;
            } else {
                resultBytes[1] = Constants.VAL_0X02;
            }
        }
        Log.i(TAG, "handleGetTPSts resultBytes = " + Arrays.toString(resultBytes));
        Util.setByteProperty(id, resultBytes);
    }

    //sample version : V2.10, we will return : Para-0: ‘2’, Para-1: ‘.’, Para-2: ‘1’, Para-3: ‘0’
    public void handleGetDisplaySoftwareVer(int id) {
        Log.i(TAG, "handleGetDisplaySoftwareVer : id = " + id);

        byte[] screenVersion = mYfveEolManager.getScreenVer();

        int resultLen = 5;
        byte[] resultBytes = new byte[resultLen];
        if (screenVersion == null || screenVersion.length == 0) {
            Log.e(TAG, "handleGetScreenVer: null screen version");
            resultBytes[0] = Constants.VAL_0X01;
        } else {
            resultBytes[0] = Constants.VAL_0X00;
            Log.i(TAG, "handleGetDisplaySoftwareVer: " + Arrays.toString(screenVersion));
            for (int i = 1; i < screenVersion.length && i < resultLen; i++) {
                resultBytes[i] = screenVersion[i];
            }
        }
        Log.i(TAG, "handleGetDisplaySoftwareVer resultBytes = " + Arrays.toString(resultBytes));
        Util.setByteProperty(id, resultBytes);
    }

    public byte[] handleGetTpVerTest() {
        return mYfveEolManager.getTpVer();
    }

    public byte[] handleGetScreenVerTest() {
        return mYfveEolManager.getScreenVer();
    }

    /*  return value. TrueTouch Config Version: 0x00C8 is most important info
        we will return 00C8

        TrueTouch Product ID: 0x00 0x0D
        Firmware Major Version: 0x01
        Firmware Minor Version: 0x04
        Revision Control Number: 0x00 0x00 0x00 0x00 0x00 0x10 0x8C 0xE2
        TrueTouch Config Version: 0x00C8
        Bootloader Major Version: 0x02
        Bootloader Minor Version: 0x04
    */
    public void handleGetTpVer(int id) {
        Log.i(TAG, "handleGetScreenVer : id = " + id);

        byte[] tpVersionBytes = mYfveEolManager.getTpVer();

        int resultLen = 7;

        byte[] resultBytes = new byte[resultLen];
        if (tpVersionBytes == null || tpVersionBytes.length == 0) {
            Log.e(TAG, "handleGetScreenVer: null screen version");
            resultBytes[0] = Constants.VAL_0X01;
        } else {
            String tpVersionStr = new String(tpVersionBytes);
            Log.i(TAG, "handleGetTpVer: tpVersionStr = " + tpVersionStr);
            String configVersionStr = "TrueTouch Config Version: ";
            int index = tpVersionStr.indexOf(configVersionStr);
            int tpVersionIndex = index + configVersionStr.length();

            resultBytes[0] = Constants.VAL_0X00;
            for (int i = 0; i < (resultBytes.length - 1); i++) {
                if ((tpVersionIndex + i) >= tpVersionBytes.length) {
                    Log.e(TAG, "handleGetTpVer: length overflow");
                    break;
                }
                resultBytes[i + 1] = tpVersionBytes[tpVersionIndex + i];
            }
        }
        Log.i(TAG, "handleGetTpVer resultBytes = " + Arrays.toString(resultBytes));
        Util.setByteProperty(id, resultBytes);
    }
}
